/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.shoppingsystem.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Victor Cifuentes (victor.cifuentes@correounivalle.edu.co)
 */
@Entity
@Table(name = "producto")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Producto.findAll", query = "SELECT p FROM Producto p"),
    @NamedQuery(name = "Producto.findById", query = "SELECT p FROM Producto p WHERE p.id = :id"),
    @NamedQuery(name = "Producto.findByNombre", query = "SELECT p FROM Producto p WHERE p.nombre = :nombre"),
    @NamedQuery(name = "Producto.findByPrecioCompra", query = "SELECT p FROM Producto p WHERE p.precioCompra = :precioCompra"),
    @NamedQuery(name = "Producto.findByPorIva", query = "SELECT p FROM Producto p WHERE p.porIva = :porIva"),
    @NamedQuery(name = "Producto.findByExistencias", query = "SELECT p FROM Producto p WHERE p.existencias = :existencias")})
public class Producto implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "nombre", nullable = false, length = 20)
    private String nombre;
    @Basic(optional = false)
    @Column(name = "precio_compra", nullable = false)
    private int precioCompra;
    @Basic(optional = false)
    @Column(name = "por_iva", nullable = false)
    private double porIva;
    @Basic(optional = false)
    @Column(name = "existencias", nullable = false)
    private int existencias;
    @Lob
    @Column(name = "imagen")
    private byte[] imagen;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "productoId")
    private Collection<Item> itemCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "productoId")
    private Collection<Descuento> descuentoCollection;
    @JoinColumn(name = "sucursal_id", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Sucursal sucursalId;
    @JoinColumn(name = "categoria_id", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Categoria categoriaId;
    @JoinColumn(name = "proveedor_id", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Proveedor proveedorId;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "productoId")
    private Collection<Detalle> detalleCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "productoId")
    private Collection<Precio> precioCollection;

    public Producto() {
    }

    public Producto(Integer id) {
        this.id = id;
    }

    public Producto(Integer id, String nombre, int precioCompra, double porIva, int existencias) {
        this.id = id;
        this.nombre = nombre;
        this.precioCompra = precioCompra;
        this.porIva = porIva;
        this.existencias = existencias;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        Integer oldId = this.id;
        this.id = id;
        changeSupport.firePropertyChange("id", oldId, id);
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        String oldNombre = this.nombre;
        this.nombre = nombre;
        changeSupport.firePropertyChange("nombre", oldNombre, nombre);
    }

    public int getPrecioCompra() {
        return precioCompra;
    }

    public void setPrecioCompra(int precioCompra) {
        int oldPrecioCompra = this.precioCompra;
        this.precioCompra = precioCompra;
        changeSupport.firePropertyChange("precioCompra", oldPrecioCompra, precioCompra);
    }

    public double getPorIva() {
        return porIva;
    }

    public void setPorIva(double porIva) {
        double oldPorIva = this.porIva;
        this.porIva = porIva;
        changeSupport.firePropertyChange("porIva", oldPorIva, porIva);
    }

    public int getExistencias() {
        return existencias;
    }

    public void setExistencias(int existencias) {
        int oldExistencias = this.existencias;
        this.existencias = existencias;
        changeSupport.firePropertyChange("existencias", oldExistencias, existencias);
    }

    public byte[] getImagen() {
        return imagen;
    }

    public void setImagen(byte[] imagen) {
        byte[] oldImagen = this.imagen;
        this.imagen = imagen;
        changeSupport.firePropertyChange("imagen", oldImagen, imagen);
    }

    @XmlTransient
    public Collection<Item> getItemCollection() {
        return itemCollection;
    }

    public void setItemCollection(Collection<Item> itemCollection) {
        this.itemCollection = itemCollection;
    }

    @XmlTransient
    public Collection<Descuento> getDescuentoCollection() {
        return descuentoCollection;
    }

    public void setDescuentoCollection(Collection<Descuento> descuentoCollection) {
        this.descuentoCollection = descuentoCollection;
    }

    public Sucursal getSucursalId() {
        return sucursalId;
    }

    public void setSucursalId(Sucursal sucursalId) {
        Sucursal oldSucursalId = this.sucursalId;
        this.sucursalId = sucursalId;
        changeSupport.firePropertyChange("sucursalId", oldSucursalId, sucursalId);
    }

    public Categoria getCategoriaId() {
        return categoriaId;
    }

    public void setCategoriaId(Categoria categoriaId) {
        Categoria oldCategoriaId = this.categoriaId;
        this.categoriaId = categoriaId;
        changeSupport.firePropertyChange("categoriaId", oldCategoriaId, categoriaId);
    }

    public Proveedor getProveedorId() {
        return proveedorId;
    }

    public void setProveedorId(Proveedor proveedorId) {
        Proveedor oldProveedorId = this.proveedorId;
        this.proveedorId = proveedorId;
        changeSupport.firePropertyChange("proveedorId", oldProveedorId, proveedorId);
    }

    @XmlTransient
    public Collection<Detalle> getDetalleCollection() {
        return detalleCollection;
    }

    public void setDetalleCollection(Collection<Detalle> detalleCollection) {
        this.detalleCollection = detalleCollection;
    }

    @XmlTransient
    public Collection<Precio> getPrecioCollection() {
        return precioCollection;
    }

    public void setPrecioCollection(Collection<Precio> precioCollection) {
        this.precioCollection = precioCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Producto)) {
            return false;
        }
        Producto other = (Producto) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "[ " + id + " ] " + nombre ;
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }
    
}
